package com.yunbiao.ybbusstopplate.utils.core;

import android.content.Context;

import com.yunbiao.ybboardapi.DeviceType;
import com.yunbiao.ybboardapi.backlight.BackLightManager;
import com.yunbiao.ybboardapi.utils.SoundContral;
import com.yunbiao.ybbusstopplate.utils.CommonUtils;


public class BackLightTool {
    private static BackLightTool backLightTool;
    private BackLightTool(){}
    public static BackLightTool getInstance(){
        if (backLightTool==null){
            backLightTool=new BackLightTool();
        }
        return backLightTool;
    }

    private static int currentVolume=0;

    public void setBackLight(Context context, String type){
        Integer broadType = CommonUtils.getBroadType();
        BackLightManager manager = BackLightManager.getInstance();
        DeviceType deviceType= DeviceType.SDMT;
        switch (broadType){
            case 3:
                deviceType= DeviceType.YISHENG;
                break;
            case 31://亿盛 M11
                deviceType= DeviceType.YISHENG_M;
                break;
            case 6://
                deviceType= DeviceType.MSTAR;
                break;
            case 0://视美泰
                deviceType= DeviceType.SDMT;
                break;

        }
        int minVolume = 0;//设备最小声音
//        try {
//            minVolume = SoundContral.getInstance().getMinVolume(context);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        if ("0".equals(type)){
            //熄屏
            currentVolume = SoundContral.getInstance().getCurrentVolume(context);
            int light = manager.enableBackLightWithType(context,false, deviceType);
            int volume = SoundContral.getInstance().getCurrentVolume(context);
            //熄屏成功并且当前声音不是静音，进行静音
            if (light==0&&volume!=minVolume){
                SoundContral.getInstance().setVolume(context,minVolume);
            }
        }else {
            //亮屏
            int light = manager.enableBackLightWithType(context,true, deviceType);
            int volume = SoundContral.getInstance().getCurrentVolume(context);
            //亮屏成功并且上次声音不为0，当前声音是静音，恢复上次声音
            if (light==1
                    &&currentVolume!=0
                    &&volume==minVolume){
                SoundContral.getInstance().setVolume(context,currentVolume);
            }
        }
    }
}
